/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAuditListCtrl', ['$scope','FwaTabsetService','WarningAddService','$modal','BaseURL','Modal','WarningService',
	                                function($scope,FwaTabsetService,WarningAddService,$modal,baseUrl,Modal,WarningService){
		$scope.warningService = WarningService;
		WarningService.initMedicalOrgInfoList();
		$scope.form = {};
		$scope.form.warnDate = {};
		var currentPage = 1;
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="clickLink(\'edit\',data)"">{{data.tranName}}</a>'+
			'</td>',
			cellTemplateView = 
				'<td>'+
				   '<a title=\'{{data.warnNoBiz}}\' ng-click="clickLink(\'warnQueryAudit.warningView\',data)"">{{data.warnNoBiz}}</a>'+
			    '</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
       $scope.columns = [
        {label:'预警编号',width:'11%',cellClass:'text-left', cellTemplate:cellTemplateView},
        {label:'预警事件名称',map:'summary',width:'15%'},
        {label:'机构名称',map:'medicalOrgName',width:'18%'},
        {label:'处理方式',map:'process',width:'11%'},
        {label:'预警时段',map:'happenDate',width:'14%'},       
        {label:'受理时间',map:'warnDate',width:'11%'},
        {label:'事件状态',map:'actName',width:'11%'},
        {label:'操作',width:'9%',cellClass:'text-left', cellTemplate:cellTemplate}
    ];
       $scope.clickLink = function (url,data){
    	   if(url==='edit'){
    		   FwaTabsetService.selectTab('warnQueryAudit.warningAuditWarn',{warnNo:data.warnNo,urlFlag:''},false);
    	   }else{
    		   FwaTabsetService.selectTab(url,{warnNo:data.warnNo,actNo:'aduit',isBack:'N'},false);
    	   }
       }

		//每页显示数
        var start = 0,
            limit = 20;
        
//        $scope.form = {
//            page:"0",
//            pageSize:"20"
//        };

        
        var params = function (){
			var warnNoBiz=null;
			if($scope.form.warnNoBiz&&!angular.isUndefined($scope.form.warnNoBiz)){
				warnNoBiz = $scope.form.warnNoBiz;
			}
			var medicalOrgCode=null;
			if($scope.form.medicalOrgCode&&!angular.isUndefined($scope.form.medicalOrgCode)){
				medicalOrgCode = $scope.form.medicalOrgCode;
			}
			var summary=null;
			if($scope.form.summary&&!angular.isUndefined($scope.form.summary)){
				summary = $scope.form.summary;
			}
			var feedbackEndDate=null;
			if($scope.form.feedbackEndDate&&!angular.isUndefined($scope.form.feedbackEndDate)){
				feedbackEndDate = $scope.form.feedbackEndDate;
			}
//			var actNo=null;
//			if($scope.form.actNo&&!angular.isUndefined($scope.form.actNo)){
//				actNo = $scope.form.actNo;
//			}
			var startDate=null;
			if($scope.form.warnDate.start&&!angular.isUndefined($scope.form.warnDate.start)){
				startDate = $scope.form.warnDate.start;
			}
			var endDate=null;
			if($scope.form.warnDate.end&&!angular.isUndefined($scope.form.warnDate.end)){
				endDate = $scope.form.warnDate.end;
			}
			
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					warnNoBiz:warnNoBiz,
					medicalOrgCode:medicalOrgCode,
					summary:summary,
					feedbackEndDate:feedbackEndDate,
//					actNo:actNo,
					startDate:startDate,
					endDate:endDate,
					start:start,
					limit:limit
			}
			return params;
		}
        
        
      //查询
        function query(){
        	WarningService.warningAuditQuery(params()).success(function(res){
                $scope.tableData = res.result;
                $scope.totalCount = res.total;
            });
        }

		//分页查询
        $scope.$on('pageTool.skipPage',function(e,v){
            start = limit*(v-1);
            $scope.form.page = start;
            //$scope.onSubmit();
            query();
        });

        //页面点击查询
        $scope.onSubmit = function(){
        	start=0;
        	$scope.currentPage=1;
        	 query();
        };
		
        //加载数据
        $scope.onSubmit();
        
         //重置
        $scope.resetBut = function(){
			$scope.form = {};
			$scope.form.warnDate = {};
		}
      //---------------批量审核-------------------
        var  approveType=['warn_approve'];
		$scope.approveList=[];
		function init(){
			WarningAddService.getHcpmsCodeList(approveType).success(function(response){
	            $scope.approveList=response.result;
			});			
		}
		init();
		/*$scope.batchWarningDatas=null;
		//批量选中的值
		 function getBatchWarning() {
			var batchWarningDatas = [];
			angular.forEach($scope.tableData,function(data,index) {
				if(data.checked){
					batchWarningDatas.push(data);
				}
			});
			if(batchWarningDatas.length>0){
				$scope.batchWarningDatas = batchWarningDatas;	
			}					
		}*/
		
        //批量审核弹窗     
		$scope.submitBatchWarning = function(){
			//getBatchWarning();
			$scope.batchWarningDatas=null;
			//批量选中的值			
				var batchWarningDatas = [];
				angular.forEach($scope.tableData,function(data,index) {
					if(data.checked){
						batchWarningDatas.push(data);
					}
				});
				if(batchWarningDatas.length>0){
					$scope.batchWarningDatas = batchWarningDatas;	
				}	
			if(!$scope.batchWarningDatas){
				 Modal.alert('请选择批量审核的数据！');
				 return;
			}
			var scope=$scope.$new();
			scope.approveList= $scope.approveList;
			scope.batchWarningDatas=$scope.batchWarningDatas;
			 var modalInstance = $modal.open({
				  templateUrl: baseUrl + 'views/hcpms/warning/warnQuery/warningAuditBatchDialog_view.html',			     
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: function($scope, $modalInstance,Modal,CommunityService){
					  $scope.communityService = CommunityService;
					  CommunityService.initCodeListDatas();					  
					  $scope.form = {};					 
					  //关闭弹窗
					  $scope.onOk = function() {
						  $modalInstance.dismiss();
					  }
					  //确定事件
					  $scope.confirm = function() {						 
						  if(!$scope.form.auditOpinion){
								Modal.alert("审核意见不能为空!");
								return false;
							}
							if($scope.form.auditOpinion === '02'&&!$scope.form.auditDesc){
								Modal.alert("不同意时,意见说明不能为空!");
								return false;
							}
							var param = {
									auditOpinion:$scope.form.auditOpinion,
									auditDesc:$scope.form.auditDesc,
									batchWarnList:$scope.batchWarningDatas
							}	 
					        $scope.form.loading=true;
							WarningAddService.auditWarnBatchProcess(param).success(function(res){
								Modal.alert(res.msg);
							  $scope.form.loading=false;
							  if(res.state=="0"){
								  $modalInstance.dismiss();
								  $scope.onSubmit();
							  }
				            });
						//$modalInstance.close($scope.form);
					  }
				  }
			 });
		 }
        //-------------批量结束
	}]);
})();